using System;
using System.Data;
using System.Configuration;
using System.Net;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using NCI.EasyObjects;
using Microsoft.Practices.EnterpriseLibrary;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.Data.Common;
using System.IO;
using System.Xml;
using System.Text;
using System.Text.RegularExpressions;

public class ConvertData
{
    public static int ConvertDatetimeToInt(DateTime  objDatetime)
    {
        int nDateReturn = 0;
        int nDay = objDatetime.Day;
        int nMonth = objDatetime.Month;
        string sYear = ConvertData.ConvertToString(objDatetime.Year);
        string sMonth = "";
        string sDay ="";

        if (nDay < 10)
            sDay = "0" + ConvertData.ConvertToString(nDay);
        else sDay = ConvertData.ConvertToString(nDay);

        if (nMonth  < 10)
            sMonth  = "0" + ConvertData.ConvertToString(nMonth );
        else sMonth = ConvertData.ConvertToString(nMonth);
        nDateReturn = ConvertData.ConvertToInt (sYear + sMonth + sDay);
        return nDateReturn;
    }
    public static int ConvertDateStringToInt(string objDatetime)
    {
        string value = "";
        try
        {
            string[] words = objDatetime.Split('/');
            for (int i = words.Length - 1; i >= 0; i--)
            {
                value = value + words[i];
            }
        }
        catch
        {
            return 0;
        }
        return ConvertToInt(value);
    }
    public static string ConvertIntToDatetimeString(int objIntDatatime)
    {
        string sYear = ConvertData.ConvertToString(objIntDatatime).Substring(0,4); 
        string sMonth = ConvertData.ConvertToString(objIntDatatime).Substring(4,2); 
        string sDay = ConvertData.ConvertToString(objIntDatatime).Substring(6,2); 
        int nLangID = Support.GetCurrentLanguage();   
        string sDatetime="";
        if (nLangID >1)
            sDatetime = sMonth +"/" + sDay + "/" + sYear ;
        else sDatetime = sDay +"/" + sMonth  + "/" + sYear ;

        return sDatetime;
    }

    public static int ConvertStringToIntDate(string sDate)
    {
        // sDate= 31/12/2010
        sDate = sDate.Replace("/", "");
        sDate = sDate.Replace("-", "");
        string sDay = sDate.Substring(0, 2);
        string sMonth = sDate.Substring(2, 2);
        string sYear = sDate.Substring(4, 4);

        int nDate = ConvertData.ConvertToInt(sYear + sMonth + sDay);


        return nDate;
    }

    public static string RemoveHTMLTag(string objText)
    {
        string text = objText;
        string pattern = "\\<.*?\\>";
        Regex regx = new Regex(pattern);
        if (regx.IsMatch(objText))
        {
            text = regx.Replace(objText, "");
        }
        return text;
    }

    public static string RemoveBadChar(string Input)
    {
        string strResult = "";
        string strTemp = Input;
        if (Input != null)
        {
            strTemp = strTemp.Replace("#", "");
            strTemp = strTemp.Replace("%", "");
            strTemp = strTemp.Replace("&", "");
            strTemp = strTemp.Replace("*", "");
            strTemp = strTemp.Replace("'", "");
            strTemp = strTemp.Replace("<", "");
            strTemp = strTemp.Replace(">", "");
            strTemp = strTemp.Replace(".", "");
            strTemp = strTemp.Replace("?", "");
            strTemp = strTemp.Replace("/", "");
            strTemp = strTemp.Replace("-", "");
            strTemp = strTemp.Replace(",", "");
            strResult = strTemp;
        }
        return strResult;
    }

    public static string ConvertStringToDB(string Input)
    {
        string strResult = "";
        string strTemp = Input;
        if (Input != null)
        {
            strTemp = strTemp.Replace("'", "''");
            strTemp = strTemp.Replace("[", "[[");
            strTemp = strTemp.Replace("]", "]]");
            strResult = strTemp;
        }
        return strResult;
    }

    public static string ConvertStringFromDB(string Input)
    {
        string strResult = "";
        string strTemp = Input;
        if (Input != null)
        {
            strTemp = strTemp.Replace("''", "'");
            strTemp = strTemp.Replace("[[", "[");
            strTemp = strTemp.Replace("]]", "]");
            strResult = strTemp;
        }
        return strResult;
    }

    public static DateTime ConvertDateFromDB(int Input)
    {
        DateTime dtResult = DateTime.Now;
        try
        {
            int intTemp = Input;
            int intYear = intTemp / 10000;
            int intMode = intTemp % 10000;
            int intMonth = intMode / 100;
            int intDay = intMode % 100;
            dtResult = new DateTime(intYear, intMonth, intDay);
        }
        catch
        {
        }
        return dtResult;
    }

    public static DateTime ConvertDateFromDB(string Value)
    {
        DateTime dtResult = DateTime.Now;
        try
        {
            int Input = Convert.ToInt32(Value);
            int intTemp = Input;
            int intYear = intTemp / 10000;
            int intMode = intTemp % 1000;
            int intMonth = intMode / 100;
            int intDay = intMode % 100;
            dtResult = new DateTime(intYear, intMonth, intDay);
        }
        catch
        {
        }
        return dtResult;
    }

    public static string ConvertToString(object objValue)
    {
        string strResult = "";
        try
        {
            strResult = Convert.ToString(objValue);
            if ( strResult == string.Empty)
                strResult = "";
        }
        catch
        {
            strResult = "";
        }
        return strResult;
    }

    public static string ConvertZeroToString(object objValue)
    {
        string strResult = "";
        try
        {
            int intObj = Convert.ToInt32(objValue);
            if (intObj == 0)
                strResult = "";
            else
                strResult = Convert.ToString(objValue);
        }
        catch
        {
            strResult = "";
        }
        return strResult;
    }

    public static int ConvertToInt(object objValue)
    {
        int intResult = 0;
        try
        {
            intResult = Convert.ToInt32(objValue);
        }
        catch
        {
            intResult = 0;
        }
        return intResult;
    }

    public static long ConvertToInt64(object objValue)
    {
        long intResult = 0;
        try
        {
            intResult = Convert.ToInt64(objValue);
        }
        catch
        {
            intResult = 0;
        }
        return intResult;
    }

    public static DateTime ConvertToDateTime(object objValue)
    {
        DateTime dtResult;
        try
        {
            dtResult = Convert.ToDateTime(objValue);
        }
        catch
        {
            dtResult = DateTime.MinValue;
        }
        return dtResult;
    }

    public static string[] convertStringToArray(string Input, string tempSeparate)
    {
        string[] arrResult;
        if (Input.Trim() == "")
        {
            arrResult = new string[] { "0" };
        }
        arrResult = Input.Split(tempSeparate.ToCharArray());
        return arrResult;
    }

    public static decimal ConvertToDecimal(object value)
    {
        decimal decResult = 0;
        try
        {
            decResult = Convert.ToDecimal(value);
        }
        catch
        {
            decResult = 0;
        }
        return decResult;
    }

    public static double ConvertToDouble(object value)
    {
        double dblResult = 0;
        try
        {
            dblResult = Convert.ToDouble(value);
        }
        catch
        {
            dblResult = 0;
        }
        return dblResult;
    }

    public static string TruncateString(string Input, int Length)
    {
        string strResult = "";
        int intMaxLength = Length;
        int intLength = Input.Length;
        try
        {
            if (intLength > intMaxLength)
                strResult = Input.Substring(0, intMaxLength);
            else
                strResult = Input;
        }
        catch
        {
            strResult = "";
        }
        return strResult;
    }

    public static bool ConvertValueToBool(int value)
    {
        bool bResult = false;
        if (value > 0)
            bResult = true;
        return bResult;
    }

    public static bool ConvertObjectToBool(object _value)
    {
        bool bResult = false;
        try
        {
            bResult = Convert.ToBoolean(_value);
        }
        catch { }
        return bResult;
    }
}
